import {
	HOST
} from '@/config'
const tools = {
	methods: {
		formatDate(now, ff) {
			console.log(now);
			let year = now.getFullYear();
			let month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;
			let date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
			let hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
			let minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
			let second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
			if (ff == 'Y-m') {
				return year + "-" + month;
			} else if (ff == 'Y-m-d') {
				return year + "-" + month + "-" + date;
			} else if (ff == 'Y.m.d') {
				return year + "." + month + "." + date;
			} else if (ff == 'Y-m-d H:i:s') {
				return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
			} else if (ff == 'Y-m-d H:i') {
				return year + "-" + month + "-" + date + " " + hour + ":" + minute;
			}
		},
		format(time, ff) {
			time = time.toString();
			if (time.length == 10) time = time * 1000;
			let d = new Date(time * 1);
			return this.formatDate(d, ff);
		},
		qs({
			url = '',
			...other
		} = {}) {
			let res = []
			for (let key in other) {
				let value = other[key]
				if (typeof value === 'object') {
					value = JSON.stringify(value)
				}
				res.push(`${key}=${value}`)
			}
			res = res.join('&')
			if (url) {
				res = {
					url: `${url}?${res}`
				}
			}
			return res
		},
		toast(title, keep) {
			uni.showToast({
				icon: 'none',
				title: title,
				mask: true,
				duration: keep || 2000
			});
		},
		loadingShow(title) {
			uni.showLoading({
				mask: true,
				title: title || '加载中..'
			});
		},
		toastClear() {
			uni.hideLoading();
		},
		alert(content, title) {
			uni.showModal({
				title: title || '温馨提示',
				confirmColor: '#916fe8',
				showCancel: false,
				content: content,
				success(res) {
					if (res.confirm) {}
				}
			})
		},
		async confirm({
			title = '温馨提示',
			content = '',
			confirmColor = '#fe8101',
			cancelText = '取消',
			confirmText = '确定',
			showCancel = true
		} = {}) {
			let [err, res] = await uni.showModal({
				title: title,
				content: content,
				confirmColor: confirmColor,
				cancelText: cancelText,
				confirmText: confirmText,
				showCancel: showCancel
			});
			return res;
		},
		getUrlParam(name, url) {
			let param = url.substr(url.indexOf('?') + 1);
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = param.match(reg);
			if (r != null) return unescape(r[2]);
			return '';
		},
		locationHref() {
			return getCurrentPages()[getCurrentPages().length - 1].route;
		},
		goTop() {
			wx.pageScrollTo({
				scrollTop: 0
			})
		},
		uploadFile(name, cb, data, params, count) {
			var that = this;
			var count = count ? count : 1;
			var chooseParams = {
				count: count,
				success: (res1) => {
					console.log(res1);
					typeof cb == "function" && cb(1, name, res1);
					var tempFilePaths = res1.tempFilePaths;
					for (var tempFileIndex in tempFilePaths) {
						uni.uploadFile({
							url: HOST + '/api/uploads',
							filePath: tempFilePaths[tempFileIndex],
							name: 'files',
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							formData: data || {},
							success: function(res3) {
								console.log(res3);
								res3 = res3.data;
								res3 = res3.trim();
								res3 = JSON.parse(res3);
								if (res3.code == 1) {
									typeof cb == "function" && cb(2, name, res3.data);
								} else {
									uni.showToast({
										icon: 'none',
										title: res3.msg
									});
								}
							},
							fail: (res2) => {
								typeof cb == "function" && cb(-2, name, res2);
							}
						})
					}
				},
				fail(res1) {
					typeof cb == "function" && cb(-1, name, res1);
				}
			};
			if (typeof params == 'object') {
				for (var i in params) chooseParams[i] = params[i];
			}
			uni.chooseImage(chooseParams)
		},
		async getCity(cb) {
			uni.getLocation({
				type: 'gcj02',
				success(res) {
					var QQMapWX = require('@/utils/qqmap-wx-jssdk.min.js');
					var qqMap = new QQMapWX({
						key: 'H3ABZ-W4UK3-RZY3H-3FPJ3-PNLES-L3BZG'
					});
					qqMap.reverseGeocoder({
						location: {
							longitude: res.longitude,
							latitude: res.latitude
						},
						success(data) {
							if (typeof cb === "function") cb(data);
						}
					})
				}
			})
		},
		get_prevPage(val) { //获取历史路由对象
			return new Promise((resolve, reject) => {
				let page = val ? (parseInt(val) + 1) : 2;
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - page];
				if (prevPage == undefined || prevPage == null) return 0;
				console.log(prevPage)
				resolve(prevPage);
			});
		},
		getRoute() {
			return getCurrentPages()[getCurrentPages().length - 1].route //获取当前页面的对象 
		},
		formatSeconds(value) { //秒转时间
			value = value.toString();
			var secondTime = parseInt(value); // 秒
			var minuteTime = 0; // 分
			var hourTime = 0; // 小时
			if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
				//获取分钟，除以60取整数，得到整数分钟
				minuteTime = parseInt(secondTime / 60);
				//获取秒数，秒数取佘，得到整数秒数
				secondTime = parseInt(secondTime % 60);
				//如果分钟大于60，将分钟转换成小时
				if (minuteTime > 60) {
					//获取小时，获取分钟除以60，得到整数小时
					hourTime = parseInt(minuteTime / 60);
					//获取小时后取佘的分，获取分钟除以60取佘的分
					minuteTime = parseInt(minuteTime % 60);
				}
			}
			var result = '';
			if (hourTime > 0) {
				result = (parseInt(hourTime) < 10 ? "0" + parseInt(hourTime) : parseInt(hourTime)) + ":";
			}
			if (minuteTime > 0) {
				result += (parseInt(minuteTime) < 10 ? "0" + parseInt(minuteTime) : parseInt(minuteTime)) + ":";
			}
			if (value == 0 || (minuteTime == 0 && secondTime == 0)) {
				return '00:00';
			} else {
				result += parseInt(secondTime) < 10 ? "0" + parseInt(secondTime) : parseInt(secondTime);
			}
			return result;
		}
	}
}


export {
	tools
}
